package com.cz.modules.jk.service.impl;

import com.cz.common.utils.ParkingUtil;
import com.cz.modules.job.task.WeatherForecastTask;
import nova.traffic.NovaTraffic;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

@Component
@Order(value = 1)
public class StartService  implements ApplicationRunner {
    private OutputStreamWriter os = null;
    private InputStream is = null;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        /* NovaTraffic novaTraffic = new NovaTraffic("192.168.2.51", 5000);//北区
        int  ret = novaTraffic.sendFile(new File("d:\\parkingNb.png"),"parkingNb.png");
        System.out.println("--------------传·1完成");
        NovaTraffic novaTraffic1 = new NovaTraffic("192.168.2.151", 5000);//南区
        int  ret4 = novaTraffic1.sendFile(new File("d:\\parkingSb.png"),"parkingSb.png");
        System.out.println("-----------------传·2完成");
       NovaTraffic novaTraffic1 = new NovaTraffic("192.168.2.151", 5000);//南区
        int  ret = novaTraffic.sendFile(new File("d:\\parkingN.png"),"parkingN.png");
        System.out.println("--------------传·1完成");
        int  ret2 = novaTraffic1.sendFile(new File("d:\\parkingS.png"),"parkingS.png");
        System.out.println("-----------------传·2完成");
        NovaTraffic novaTraffic2 = new NovaTraffic("192.168.2.51", 5000);//北区
        int  ret3 = novaTraffic2.sendFile(new File("d:\\parkingNb.png"),"parkingNb.png");
        System.out.println("-------------传·3完成");
        NovaTraffic novaTraffic3 = new NovaTraffic("192.168.2.151", 5000);//南区
        int  ret4 = novaTraffic3.sendFile(new File("d:\\parkingSb.png"),"parkingSb.png");
        System.out.println("-------------传·4完成");*/
        new SocketThread().start();//=========================================建立长连接
        //==================================更新一下天气预报
        String url = "https://api.seniverse.com/v3/weather/daily.json?key=SqgBoqgD2Xz1mPnG6&location=Taiyuan&language=zh-Hans&unit=c&start=0&days=2";
        String resultGet = WeatherForecastTask.doHttpGet(url);
        WeatherForecastTask.jiexi(resultGet);
    }
    /**
     * 发送心跳包
     */
    /*public void sendHeartbeat() {
        try {
            String heartbeat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><QDELITS type=\"ITS\"><ITS id=\"192.168.2.50\" Port=\"9999\"><CMD type=\" LED_NET_STATUS\" id=\"1\"/></ITS></QDELITS>";
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            Thread.sleep(10 * 1000);// 10s发送一次心跳
                            pw.write(heartbeat);
                            pw.flush();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    private class SocketThread extends Thread {

        @Override
        public void run() {
            long startTime = System.currentTimeMillis();
            //sendHeartbeat();
            while (true) {
                try {
                    if (ParkingUtil.socket == null || ParkingUtil.socket.isClosed()) {
                        ParkingUtil.socket = new Socket("192.168.0.4", 50100); // 连接socket
                        os= new OutputStreamWriter(ParkingUtil.socket.getOutputStream(), StandardCharsets.UTF_8);
                        ParkingUtil.pw=new PrintWriter(os);
                    }
                    Thread.sleep(100);
                    is = ParkingUtil.socket.getInputStream();
                    int size = is.available();
                    if (size <= 0) {
                        if ((System.currentTimeMillis() - startTime) >  15* 10 * 1000) { // 如果超过30秒没有收到服务器发回来的信息，说明socket连接可能已经被远程服务器关闭
                            ParkingUtil.socket.close(); // 这时候可以关闭socket连接
                            startTime = System.currentTimeMillis();
                        }
                        continue;
                    } else {
                        startTime = System.currentTimeMillis();
                    }
                    byte[] resp = new byte[size];
                    is.read(resp);
                    String response = new String(resp, "utf-8");
                    //System.out.println("====================="+response);
                } catch (Exception e) {
                    e.printStackTrace();
                    try {
                        ParkingUtil.socket.close();
                        is.close();
                        os.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
    }

}
